package com.dex4d.ui.qr.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dex4d.R;
import com.dex4d.core.Web2Activity;
import com.dex4d.ui.qr.model.PayData;
import com.dex4d.ui.qr.utils.AddressUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EasyPayDoneFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParamUrl;
    private PayData payData;


    @BindView(R.id.label_pay_flag)
    TextView label_pay_flag;

    @BindView(R.id.txt_pay_amount)
    TextView txt_pay_amount;

    @BindView(R.id.txt_from)
    TextView txt_from;

    @BindView(R.id.txt_to)
    TextView txt_to;

    @BindView(R.id.txt_fee)
    TextView txt_fee;

    @BindView(R.id.txt_hash)
    TextView txt_hash;

    public EasyPayDoneFragment() {
        // Required empty public constructor
    }


    public static EasyPayDoneFragment newInstance(String param1, PayData payData) {
        EasyPayDoneFragment fragment = new EasyPayDoneFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putParcelable(ARG_PARAM2, payData);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParamUrl = getArguments().getString(ARG_PARAM1);
            payData = getArguments().getParcelable(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_easy_pay_done, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        showUI(payData);
    }

    private void showUI(PayData payData) {
        String info = payData.getDesc(getActivity());
        label_pay_flag.setText(info);
        txt_pay_amount.setText(payData.getAmount(getActivity()));
        txt_from.setText(AddressUtils.convertAddress(payData.from));
        txt_to.setText(AddressUtils.convertAddress(payData.to));
        txt_fee.setText(payData.txFee);
        txt_hash.setText(AddressUtils.convertAddress(payData.txHash));
    }

    @OnClick(R.id.txt_hash)
    public void toWeb2() {
        Web2Activity.openWeb2(getActivity(), "https://rinkeby.etherscan.io/tx/" + payData.txHash);
    }
}
